using Basis;
using System.Collections.Generic;
using UnityEngine;

namespace NL.Advanced
{
    public interface ITargetIgnor
    {
        void IgnorAdd(GameObject target);
        bool IgnorHas(GameObject target);

        void IgnorClear();

        IEnumerable<GameObject> IgnorAll { get; }
    }
    public class TargetIgnor : MonoBehaviour, ITargetIgnor
    {
        IEnumerable<GameObject> ITargetIgnor.IgnorAll => Ignors;
        List<GameObject> Ignors = new List<GameObject>();

        public void IgnorAdd(GameObject target) => Ignors.Add(target);
        public bool IgnorHas(GameObject target) => Ignors.Contains(target);
        public void IgnorClear() => Ignors.Clear();


        private void OnEnable()
        {
            Ignors.Clear();
        }
        private void OnDisable()
        {
            Destroy(this);
        }

    }
    public static class TargetIgnorEx
    {
        public static GameObject IGNORAdd(this GameObject target, IEnumerable<GameObject> all)
        {
            var comp = target.GETComponent<TargetIgnor>();

            if (comp != null)
            {
                foreach (var a in all)
                {
                    comp.IgnorAdd(a);
                }
            }
            return target;
        }
        public static GameObject IGNORAdd(this GameObject target, GameObject who)
        {
            target.GETComponent<TargetIgnor>()?.IgnorAdd(who);

            return target;
        }
        public static GameObject IGNORClear(this GameObject target, GameObject who)
        {
            target.GETComponent<TargetIgnor>()?.IgnorClear();

            return target;
        }
        public static bool IGNORHas(this GameObject target, GameObject who)
        {
            var comp = target.GETComponent<TargetIgnor>();
            if (comp == null)
                return false;
            else
                return comp.IgnorHas(who); ;
        }
    }
}

